package com.renjiyue.GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//事件监听
public class GUIDome08 {
    public static void main(String[] args) {
        Frame frame = new Frame("TestActionEvent");
        Button button = new Button("press Me");
        //创建一个监听对象
        MyActionListener listener = new MyActionListener();
        /*
        把监听加入到按钮里面，监听按钮的动作
        当按钮触发打击事件时，就会返回一个监听对象e
        然后就会自动执行actionPerformed方法
         */
        button.addActionListener(listener);
        frame.add(button,BorderLayout.CENTER);
        frame.pack();

        addwindowClosingEvent(frame);
        frame.setVisible(true);
    }
    //点击窗体上的关闭按钮关闭窗体
    private static void addwindowClosingEvent(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}
class MyActionListener implements ActionListener{
    //重写ActionListener接口里面的actionPerformed(ActionEvent e)方法

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("A Button has been Pressed");
    }
}